'''This file has the structure of the main menus of the game. This piece of
the program is completely horrible and should not be looked at without proper UV
protection.
'''

from pygame import font, rect

from input   import *
from ui      import *
from options import *

from os      import listdir
from os.path import join

import main

def input_setter_generator(player, input_id):
    def f():
        option['inputs'][player] = input_id
    return f

def input_setup_generator(player):
    def f():
        menu = Menu()
        for x in range(0, len(inputs)):
            menu.add_item(Button(inputs[x].description, input_setter_generator(player, x)))
        menu.add_item(Button("None", input_setter_generator(player, None)))
        menu.add_item(Button("Cancel"))
        menu.create_layout(rect.Rect(200, 100, 400, 400))    
        while True:
            f = menu.run()
            if f == return_function:
                return
            f()
            return
    return f

def player_setup_menu():
    menu = Menu()
    buttons = []
    buttons.append(Button("P1", input_setup_generator(0)))
    buttons.append(Button("P2", input_setup_generator(1)))
    buttons.append(Button("P3", input_setup_generator(2)))
    buttons.append(Button("P4", input_setup_generator(3)))
    for b in buttons:
        menu.add_item(b)
    menu.add_item(Button("Return"))
    menu.create_layout(rect.Rect(200, 100, 400, 400))
    while True:
        for x in xrange(0, len(buttons)):
            if option['inputs'][x] is None:
                buttons[x].text = "Player %d: Off" %(x+1)
            else:
                buttons[x].text = "Player %d: %s" %(x+1, inputs[option['inputs'][x]].description)
        f = menu.run()
        if f == return_function:
            return
        f()
        

def main_menu():
    menu = Menu()
    menu.add_item(Button("New Game", new_game_menu))
    #menu.add_item(Button("Options", options_menu.run)
    #menu.add_item(Button("Credits", show_credits)
    menu.add_item(Button("Quit"))
    menu.create_layout(rect.Rect(200, 100, 400, 400))
    while True:
        f = menu.run()
        if f == return_function:
            return
        f()

def forward_map_list_start():
    global map_list_start, map_list
    map_list_start += 3
    if map_list_start >= len(map_list):
        map_list_start = 0

def rewind_map_list_start():
    global map_list_start
    map_list_start -= 3
    if map_list_start < 0:
        map_list_start = 0

def map_switch_function(map_name):
    return lambda: map_name

def map_select_menu():
    global map_list_start, map_list
    map_list = listdir(join("data", "maps"))
    map_list.sort()
    map_list_start = 0
    while True:
        menu = Menu()
        if map_list_start > 0:
            menu.add_item(Button("Back", rewind_map_list_start))
        end = map_list_start+3
        if end > len(map_list):
            end = len(map_list)
        for m in xrange(map_list_start, end):
            menu.add_item(Button(map_list[m], map_switch_function(map_list[m])))
        if end < len(map_list):
            menu.add_item(Button("Next", forward_map_list_start))
        menu.add_item(Button("Cancel", return_function))
        menu.create_layout(rect.Rect(200, 100, 400, 400))

        print map_list_start, end
        f = menu.run()
        if f == return_function:
            return()
        if f != forward_map_list_start and f != rewind_map_list_start:
            option['map'] = f()
            return()
        else:
            f()

def new_game_menu():
    menu = Menu()
    menu.add_item(Button("Start", main.main_loop))
    menu.add_item(Button("Player setup", player_setup_menu))
    map_button = Button("Map: "+option["map"], map_select_menu)
    menu.add_item(map_button)
    menu.add_item(Button("Return"))
    menu.create_layout(rect.Rect(200, 100, 400, 400))
    while True:
        map_button.text = "Map: "+option["map"]
        f = menu.run()
        if f == return_function:
            return
        f()

